package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.vo.ServiceItemAuditVO;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static cn.wolfcode.car.business.domain.ServiceItem.CARPACKAGE_YES;

@Service
//@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private RuntimeService runtimeService;


    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        ServiceItem newItem = new ServiceItem();

        newItem.setName(serviceItem.getName());
        newItem.setOriginalPrice(serviceItem.getOriginalPrice());
        newItem.setDiscountPrice(serviceItem.getDiscountPrice());
        newItem.setCarPackage(serviceItem.getCarPackage());
        newItem.setInfo(serviceItem.getInfo());
        newItem.setServiceCatalog(serviceItem.getServiceCatalog());


        //在整加的时候给里面增加时间,只定义在这里,数据库能看到,但浏览器增加页面不用显示
        serviceItem.setCreateTime(new Date());

        //判断数据是否是套餐,如果是套餐设置审核状态为初始化, 如果不是套餐设置状态为无需申请
        if (serviceItem.getCarPackage().equals(CARPACKAGE_YES)){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);

            //如果是套餐,且又是初始化的,就说明没有审核过,所以就显示不能上架
            serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        }else {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);

            //否者不是套餐,不用审核,可以上架
            serviceItem.setSaleStatus(ServiceItem.SALESTATUS_ON);
        }

        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem oldItem = this.get(serviceItem.getId()); //直接在本类中调用查询一个的方法
        //和上面代码一样的操作:ServiceItem oldItem = serviceItemMapper.selectByPrimaryKey(serviceItem.getId());

        // 如果是审核中 或者上架状态 抛出异常
        if (ServiceItem.AUDITSTATUS_AUDITING.equals(oldItem.getAuditStatus()) || ServiceItem.SALESTATUS_ON.equals(oldItem.getSaleStatus()) ){
            throw new BusinessException("非法操作");
        }
        serviceItem.setAuditStatus(oldItem.getAuditStatus()); //把原有的审核状态还是设置给当前审核状态

        // 判断数据如果是套餐并且审核状态是审核通过的状态,把审核状态修改为初始状态
        if (ServiceItem.CARPACKAGE_YES.equals(oldItem.getCarPackage()) && ServiceItem.AUDITSTATUS_APPROVED.equals(oldItem.getAuditStatus())){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
            //是套餐,且又是初始化的,就说明没有审核过,所以就显示不能上架
            serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);

        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }


    // 上架
    @Override
    public void saleOn(Long id) {
        // 根据id 去查询服务单项
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);
        // 判断数据如果已经是上架了, 抛出异常
        if (ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus())){
            throw new BusinessException("已经上架了");
        }

        // 判断如果数据是套餐并且审核状态是 没有(非)审核通过的,抛出异常
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage()) && !ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())) {
            throw new BusinessException("非法操作");
        }

        // 以上不是就 修改状态为上架状态
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_ON);

    }

    // 下架
    @Override
    public void saleOff(Long id) {
        // 根据id 去查询服务单项
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);

        // 判断数据如果已经是下架了, 抛出异常
        if (ServiceItem.SALESTATUS_OFF.equals(serviceItem.getSaleStatus())){
            throw new BusinessException("已经下架了");
        }

        // 以上不是就 修改状态为下架状态
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_OFF);

    }


    /**
     *    发起审核操作
     */
    @Override
    @Transactional
    public void startAudit(ServiceItemAuditVO vo) {
        // 做合理化校验
        // 根据id 把单项查询出来
        ServiceItem serviceItem = get(vo.getId());
        // 判断如果是非套餐抛出异常
        if (ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage())){
            throw new BusinessException("非法操作,非套餐不能发起审核");
        }
        // 判断 状态如果是审核中 或是审核通过 抛出异常
        if (ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus()) ||
                ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("非法操作,审核中或是审核通过不能发起审核");
        }

        // 把数据保存到CarPackageAudit 对象中
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        carPackageAudit.setInfo(vo.getInfo());
        carPackageAudit.setAuditorId(vo.getDirector()); //审核人(店长
        carPackageAudit.setBpmnInfoId(vo.getBpmnInfoId());
        carPackageAudit.setCreateTime(new Date());
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        carPackageAudit.setServiceItemId(vo.getId());
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        carPackageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        carPackageAuditMapper.insert(carPackageAudit);

        //设置流程变量 店长,财务,折扣金额
        HashMap<String, Object> map = new HashMap<>();
        map.put("director",vo.getDirector()); //往map存 这个审核人店长(店长每次都要先经过他审核)
        if (vo.getFinance() != null){ //如果审核人财务不为空,往map存
            map.put("finance",vo.getFinance());
        }

        // 在activiti 中是不支持 BigDecimal类型
        map.put("discountPrice",serviceItem.getDiscountPrice().longValue());

        // 调用activiti api 去启动流程
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(vo.getBpmnInfoId());
        ProcessInstance processInstance = runtimeService.
                startProcessInstanceByKey(bpmnInfo.getActProcessKey(), carPackageAudit.getId().toString(), map);
        // 设置流程id到carPackageAudit 当中
        carPackageAudit.setInstanceId(processInstance.getId());
        carPackageAuditMapper.updateByPrimaryKey(carPackageAudit);

        // 修改服务单项状态为审核中的状态
        serviceItemMapper.changAuditStatus(vo.getId(),ServiceItem.AUDITSTATUS_AUDITING);

    }


}












